app.controller('typeTemplateController', function ($scope, $controller,typeTemplateService,brandService,specificationService) {
    $controller('baseController', {$scope: $scope});
    $scope.findAll = function () {
        typeTemplateService.findAll().success(function (response) {
            $scope.list = response;
        });
    };
    $scope.findByPage = function (pageNum, pageSize) {
        typeTemplateService.findByPage(pageNum, pageSize).success(function (response) {
            $scope.list = response.rows;
            $scope.reload = false;
            $scope.paginationConf.totalItems = response.total;
            setTimeout(function () {
                $scope.reload = true;
            }, 100);
        });
    };
    $scope.findById = function (id) {
        typeTemplateService.findById(id).success(function (response) {
            $scope.entity = response;
            $scope.entity.brandIds=JSON.parse(response.brandIds);
            $scope.entity.specIds=JSON.parse(response.specIds);
            $scope.entity.customAttributeItems=JSON.parse(response.customAttributeItems);
        });
    };
    $scope.search = function (pageNum, pageSize) {
        typeTemplateService.search(pageNum, pageSize, $scope.searchEntity).success(function (response) {
            $scope.list = response.rows;
            $scope.reload = false;
            $scope.paginationConf.totalItems = response.total;
            setTimeout(function () {
                $scope.reload = true;
            }, 100);
        });
    };
    $scope.save = function () {
        if ($scope.entity.id != null) {
            typeTemplateService.update($scope.entity).success(function (response) {
                if (response.success) {
                    $scope.reloadList();
                } else {
                    alert(response.message);
                }
            });
        } else {
            if ($scope.entity.name==null) {
                alert("表单不能为空");
            }else{
                typeTemplateService.save($scope.entity).success(function (response) {
                    if (response.success) {
                        $scope.paginationConf.totalItems = $scope.paginationConf.totalItems + 1;
                    } else {
                        alert(response.message);
                    }
                });
            }
        }
    };
    $scope.delete = function () {
        var ids = $.param($scope.checkedIds, true)
        typeTemplateService.delete(ids).success(function (response) {
            if (response.success) {
                $scope.paginationConf.totalItems = $scope.paginationConf.totalItems - $scope.checkedIds.length;
                $scope.checkedIds.ids = [];
            } else {
                alert(response.message);
            }
        });
    };
    $scope.addTableRow=function(){
        $scope.entity.customAttributeItems.push({});
    }
    $scope.deleteTableRow=function(index){
        $scope.entity.customAttributeItems.splice(index,1);
    }
    $scope.brandList={data:[]};
    $scope.findBrandList=function(){
        brandService.findOptions().success(function (response) {
            $scope.brandList={data:response};
        });
    }
    $scope.specificationList={data:[]};
    $scope.findSpecificationList=function(){
        specificationService.findOptions().success(function (response) {
            $scope.specificationList={data:response};
        });
    }
});